package com.changgou.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.service.PageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageServiceImpl implements PageService {

  @Autowired
  private SpuFeign spuFeign;

  @Autowired
  private CategoryFeign categoryFeign;

  @Autowired
  private SkuFeign skuFeign;

  @Value("${pagepath}")
  private String pagepath;

  @Autowired          //thymeleaf的模板引擎
  private TemplateEngine templateEngine;

  @Override
  public void generateItemPage(String spuId) {
    //(1)创建上下文
    Context context = new Context();
    //定义个静态方法,获取相关数据
    Map<String, Object> dataModel = this.findData(spuId);
    context.setVariables(dataModel); //数据模型放入上下文

    // (2) 准备文件: 生成的文件放在哪里
    File dir = new File(pagepath);
    if (!dir.exists()) {//dir 判断此目录是否存在
      //创建目录
      dir.mkdirs();
    }
    File dest = new File(dir, spuId + ".html");//生成的页面文件,写入的位置

    // (3)生成页面
    PrintWriter writer = null;
    try {
      writer = new PrintWriter(dest, "UTF-8");
      //执行模板引擎,参数1: item为模板生成页面(通常放在templates下),参数2: context上下文, 参数3:writer输出对象
      templateEngine.process("item", context, writer);
      System.out.println("测试生成页面" + spuId + ".html");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      //关闭流
      try {
        writer.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  /**
   * 获取生成静态化页面所需要的数据
   *
   * @param spuId
   * @return
   */
  private Map<String, Object> findData(String spuId) {
    Map<String, Object> resultMap = new HashMap<>();
    //1. 获取spu信息
    Result<Spu> spuResult = spuFeign.findById(spuId);
    Spu spu = spuResult.getData();
    //2. 获取图片信息
    if (spu != null) {
      if (!StringUtils.isEmpty(spu.getImages())) {
        //getImages 存放多个图片以逗号分隔的
        resultMap.put("imageList", spu.getImages().split(","));
      }
    }
    //3.获取商品分类信息1,2,3级分类
    Category category1 =categoryFeign.findById(spu.getCategory1Id()).getData();
    resultMap.put("category1", category1);
    Category category2 =categoryFeign.findById(spu.getCategory2Id()).getData();
    resultMap.put("category2", category2);
    Category category3 =categoryFeign.findById(spu.getCategory3Id()).getData();
    resultMap.put("category3", category3);
    //4. 获取sku相关信息
    List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
    resultMap.put("skuList",skuList);
    resultMap.put("specificationList", JSON.parseObject(spu.getSpecItems(),Map.class));
    return resultMap;
  }
}
